<?php
  require_once("conf/configuration.php");
  
  $username = $_POST["username"];
  $password = $_POST["password"];
  
  if (!file_exists(DATABASE) && strlen($username) >0 && strlen($password)>0) {
    $db = new SQLiteDatabase(DATABASE);
    $usertable = $db->query("create table users(id integer primary key, user text, passwd text, admin integer, dir text, force_dir integer);");
    $adduser = $db->query("insert into users values(1,'" . $username . "','" . md5($password) . "',1, '', 0);");
    $feeds = $db->query("create table feeds(id integer primary key, url text, user integer);");
    
    if($usertable !== false && $adduser !== false) {
      $message = "Success!";
    }
    
    else {
      $message = "Failure :(";
    }
  }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>wTorrent</title>
    <meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
    <style type="text/css" media="screen">@import "css/iui.css";</style>
    <script type="application/x-javascript" src="javascript/iui.js"></script>
  </head>
  <body>
    
    <div class="toolbar">
        <h1 id="pageTitle"></h1>
    </div>
    
    <form title="TorrenTouch" class="panel" action="install.php" target="_self" method="POST" selected="true">
      <h2>Add User</h2>
        <fieldset>
          <div class="row">
            <label>User</label>
            <input type="text" name="username" value=""/>
          </div>
          <div class="row">
            <label>Password</label>
            <input type="password" name="password" value=""/>
          </div>
        </fieldset>
        <?php
          if (strlen($username) >0 && strlen($password)>0) {
        ?>
          <fieldset>
            <div class="row">
              <a href="index.php" target="_self"><?= $message ?></a>
            </div>
          </fieldset>
        <?php
          }
        ?>
        <input type="submit" name="submit" value="Add User"/>
    </form>

  </body>
</html>